package com.woniu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.entity.PageResult;
import com.woniu.entity.ServiceRecordEn;
import com.woniu.mapper.ServiceRecordMapper;
import com.woniu.pojo.ServiceRecord;
import com.woniu.pojo.Team;
import com.woniu.service.IServiceRecordService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.vo.ServiceRecordVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rsw
 * @since 2022-10-18
 */
@Service
public class ServiceRecordServiceImpl extends ServiceImpl<ServiceRecordMapper, ServiceRecord> implements IServiceRecordService {

    @Resource
    private ServiceRecordMapper serviceRecordMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public PageResult searchRecord(String currentPage, String pageSize, ServiceRecordVo serviceRecordVo) throws JsonProcessingException {

       /* QueryWrapper<ServiceRecordEn> wrapper=new QueryWrapper();*/

        int currentPag = Integer.parseInt(currentPage);
        int pageSiz = Integer.parseInt(pageSize);

        Page<ServiceRecordEn> recordPage = new Page<>(currentPag, pageSiz);
        PageResult pageResult = new PageResult();






        ObjectMapper mapper = new ObjectMapper();
        // 先从redis缓存中查询
        String recordListJson = stringRedisTemplate.opsForValue().get("recordList");


        if(recordListJson!= null&&1!=1){
            IPage<ServiceRecordEn> recordEnIPageList = mapper.readValue(recordListJson, new TypeReference<IPage<ServiceRecordEn>>(){});
            pageResult.setTotal(recordEnIPageList.getTotal());
            pageResult.setData(recordEnIPageList.getRecords());
            return pageResult;
        }

//如果不存在
        IPage<ServiceRecordEn> recordIPage =serviceRecordMapper.selectPageRecord(recordPage,serviceRecordVo);
        pageResult.setTotal(recordIPage.getTotal());
        pageResult.setData(recordIPage.getRecords());
        String asString = mapper.writeValueAsString(recordIPage);
        stringRedisTemplate.opsForValue().set("recordList", asString);

            return pageResult;
        }


    }

